package com.home.cca.network;

import com.google.gson.Gson;
import com.home.cca.model.Order;
import com.home.cca.model.Result;

import java.io.IOException;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.Request;

public class SendOrdersRequest {

    private static final String URL = ApiClient.getBaseUrl() + "/SupplierManager/PSOrderMgr/SubmitSupplierSendOrder";

    public static void send(List<Order> orders) throws IOException {
        for (Order order : orders) {
            Order deliver = DeliverOrdersRequest.deliver(order);
            if (deliver.getMatList() == null || deliver.getMatList().isEmpty()) {
                throw new IOException("查询发货信息异常");
            }
            send(deliver);
        }
    }

    /**
     * 订单发货
     */
    public static void send(Order order) throws IOException {
        // 构建请求体
        FormBody.Builder formBuilder = new FormBody.Builder()
                .add("annexFile", "")
                .add("submitVM[0][OrderId]", String.valueOf(order.getId()))
                .add("submitVM[0][SendPeople]", "刘凤英") // 注意：URL编码后的中文字符
                .add("submitVM[0][TransPeople]", "刘凤英") // 注意：URL编码后的中文字符
                .add("submitVM[0][TransPhone]", "15930935383")
                .add("submitVM[0][ArriveTime]", order.getArriveTime().substring(0, 10));

        for (int i = 0; i < order.getMatList().size(); i++) {
            Order.Material material = order.getMatList().get(i);
            formBuilder.add("submitVM[0][SendOrderMatVMs][" + i + "][OrderDetailId]", String.valueOf(material.getOrderDetailId()))
                    .add("submitVM[0][SendOrderMatVMs][" + i + "][MatExpiration]", String.valueOf(material.getMatExpiration()))
                    .add("submitVM[0][SendOrderMatVMs][" + i + "][Qty]", String.valueOf(material.getQty()))
                    .add("submitVM[0][SendOrderMatVMs][" + i + "][ProDate]", order.getBuyDate().substring(0, 10))
                    .add("submitVM[0][SendOrderMatVMs][" + i + "][ProSerial]", "241212190219");
        }

        // 构建请求
        FormBody formBody = formBuilder.build();

        Request request = new Request.Builder()
                .url(URL)
                .post(formBody)
                .addHeader("Accept", "*/*")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Connection", "keep-alive")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Cookie", ApiClient.getCookie())
                .addHeader("Origin", ApiClient.getBaseUrl())
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36")
                .addHeader("X-Requested-With", "XMLHttpRequest")
                .build();

        String execute = ApiClient.execute(request);
        Result<?> res = new Gson().fromJson(execute, Result.class);
        if (!res.isSuccess()) {
            throw new IOException(res.getMessage());
        }

    }
}